﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WhittleList.Bll;
using WhittleList.Tests.Helpers;
using WhittleList.Tests.Mocks;

namespace WhittleList.Tests.Bll
{
    [TestClass]
    public class ProjectTests
    {
        [TestMethod]
        public void Project_IdSignatureTest()
        {
            var prop = typeof(Project).GetProperty("Id");
            Assert.IsNotNull(prop);
            prop.AssertReadyOnly();
            prop.AssertInt();
        }

        [TestMethod]
        public void Project_SiteIdSignatureTest()
        {
            var prop = typeof(Project).GetProperty("SiteId");
            Assert.IsNotNull(prop);
            prop.AssertReadyWrite();
            prop.AssertInt();
        }

        [TestMethod]
        public void Project_NameSignatureTest()
        {
            var prop = typeof(Project).GetProperty("Name");
            Assert.IsNotNull(prop);
            prop.AssertReadyWrite();
            prop.AssertString();
        }

        [TestMethod]
        public void Project_SequenceSignatureTest()
        {
            var prop = typeof(Project).GetProperty("Sequence");
            Assert.IsNotNull(prop);
            prop.AssertReadyWrite();
            prop.AssertInt();
        }

        [TestMethod]
        public void Project_CIDSignatureTest()
        {
            var prop = typeof(Project).GetProperty("CID");
            prop.AssertReadyOnly();
            prop.AssertBinary();
        }


        [TestMethod]
        public void Project_IsValidSignatureTest()
        {
            var prop = typeof(Project).GetProperty("IsValid");
            prop.AssertReadyOnly();
            prop.AssertBool();
        }

        [TestMethod]
        public void Project_CreatedByIdSignatureTest()
        {
            var prop = typeof(Project).GetProperty("CreatedById");
            prop.AssertReadyOnly();
            prop.AssertInt();
        }

        [TestMethod]
        public void Project_CreatedSignatureTest()
        {
            var prop = typeof(Project).GetProperty("Created");
            prop.AssertReadyOnly();
            prop.AssertDateTime();
        }

        [TestMethod]
        public void Project_LastUpdatedByIdSignatureTest()
        {
            var prop = typeof(Project).GetProperty("LastUpdatedById");
            prop.AssertReadyOnly();
            prop.AssertInt();
        }

        [TestMethod]
        public void Project_LastUpdatedSignatureTest()
        {
            var prop = typeof(Project).GetProperty("LastUpdated");
            prop.AssertReadyOnly();
            prop.AssertDateTime();
        }

        [TestMethod]
        public void Project_CheckConcurrent_Test()
        {
            Random rnd = new Random();
            var cid = MockHelpers.CreateBinary(rnd);

            Project tmp = new FakeProject(1, "Test Name", "Summary", MockHelpers.CreateBinary(rnd)).Object;

            Assert.IsFalse(tmp.CheckConcurrent(cid));
            Assert.IsTrue(tmp.CheckConcurrent(tmp.CID));

            var testCid = new System.Data.Linq.Binary(tmp.CID.ToArray());
            Assert.IsTrue(tmp.CheckConcurrent(testCid));

        }

        /*[TestMethod]
        public void Project_OnInserted_SetsUserIdCorrectly()
        {
            var Project = new Project();
            var fakeUser = new FakeUser(1, null, null, null, null);

            Project.OnInserted(fakeUser.Object);

            Assert.AreEqual(1, Project.CreatedById);
            Assert.AreEqual(1, Project.LastUpdatedById);
        }

        [TestMethod]
        public void Project_OnInserted_SetsTimeCorrectly()
        {
            var tmp = DateTime.Now;

            var Project = new Project();
            var fakeUser = new FakeUser(1, null, null, null, null);

            Project.OnInserted(fakeUser.Object);

            Assert.IsTrue(tmp <= Project.Created);
            Assert.IsTrue(tmp <= Project.LastUpdated);

            Assert.IsTrue(DateTime.Now >= Project.Created);
            Assert.IsTrue(DateTime.Now >= Project.LastUpdated);
        }

        [TestMethod]
        public void Project_OnUpdated_SetsUserIdCorrectly()
        {
            var Project = new Project();
            var fakeUser = new FakeUser(1, null, null, null, null);

            Project.OnUpdated(fakeUser.Object);

            Assert.AreEqual(0, Project.CreatedById);
            Assert.AreEqual(1, Project.LastUpdatedById);
        }

        [TestMethod]
        public void Project_OnUpdated_SetsTimeCorrectly()
        {
            var tmp = DateTime.Now;

            var Project = new Project();
            var fakeUser = new FakeUser(1, null, null, null, null);

            Project.OnUpdated(fakeUser.Object);

            Assert.AreEqual(DateTime.MinValue, Project.Created);

            Assert.IsTrue(tmp <= Project.LastUpdated);
            Assert.IsTrue(DateTime.Now >= Project.LastUpdated);
        }*/

        [TestMethod]
        public void Project_Name_Required()
        {
            var Project = new Project();
            Assert.AreEqual(1, Project.GetRuleViolations().Where(x => x.PropertyName == "Name").Count());
            Project.Name = "Test";
            Assert.AreEqual(0, Project.GetRuleViolations().Where(x => x.PropertyName == "Name").Count());
        }

        [TestMethod]
        public void Project_SiteId_Required()
        {
            var Project = new Project();
            Assert.AreEqual(1, Project.GetRuleViolations().Where(x => x.PropertyName == "SiteId").Count());
            Project.SiteId = 1;
            Assert.AreEqual(0, Project.GetRuleViolations().Where(x => x.PropertyName == "SiteId").Count());
        }
    }
}
